package com.df.bpm.utils;

import com.df.bpm.dto.BusinessInformationDTO4Search;
import com.df.bpm.dto.InformatizationRequirementDetailsDTO;
import com.df.bpm.entity.BusinessLocations;
import com.df.bpm.utils.businessViewerV2.Main;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;


public class Db_tester {

    public static void main(String[] args) throws Exception {
        Db_tester tester=new Db_tester();
        MLlocation temp=new MLlocation();
        temp.setRid("2.04.04.0441-001911180012002");
        temp.setLocation("A031@S01-L02-NPM-M2");
        tester.updateMLlocation(temp);
//        System.out.println(temp);
    }

    private Integer updateMLlocation(MLlocation temp) {
        RestTemplate restTemplate = new RestTemplate();
        String url="http://localhost:9022/material/location";
        HttpEntity<MLlocation> request = new HttpEntity<>(temp, null);
        ResponseEntity<?> response = restTemplate.exchange(
                url,
                HttpMethod.PUT,
                request, // 或者使用 HttpEntity 来添加请求头等信息
                Integer.class
        );
        return (Integer) response.getBody();
    }

    public Integer deleteBusinessEntirelyById(Integer businessIndex) {
        RestTemplate restTemplate = new RestTemplate();
//        String tempString=Main.printNonNullFields(temp);
        String url=Main.server+"businessInformationV2/entirelyById?businessInformationIndex="+businessIndex;
        ResponseEntity<Integer> response = restTemplate.exchange(
                url,
                HttpMethod.DELETE,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<Integer>() {}
        );
        return response.getBody();
    }

    public Integer updateLocation(BusinessLocations temp) {
        if(temp.getBusinessLocationsIndex()!=null
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessInformationV2/businessLocations";
            HttpEntity<BusinessLocations> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public BusinessLocations addLocation(BusinessLocations temp) {
        if(temp.getSuperior()!=null
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessInformationV2/businessLocations";
            HttpEntity<BusinessLocations> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.POST,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    BusinessLocations.class
            );
            return (BusinessLocations) response.getBody();
        }
        return null;
    }

    public List<BusinessInformationDTO4Search> getBusinessInformationDTO4Search(BusinessInformationDTO4Search temp) {
        RestTemplate restTemplate = new RestTemplate();
        String tempString=Main.printNonNullFields(temp);
        String url=Main.server+"businessInformationV2/businessInformationDTO4Search?"+tempString;
        ResponseEntity<List<BusinessInformationDTO4Search>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<BusinessInformationDTO4Search>>() {}
        );
        return response.getBody();
    }

    public InformatizationRequirementDetailsDTO getDetailsDto(Integer informatizationRequirementIndex) {
        RestTemplate restTemplate = new RestTemplate();
        String url= Main.server+"informatization/informatizationRequirementDetailsDTO/informatizationRequirementIndex?informatizationRequirementIndex="+informatizationRequirementIndex;
        ResponseEntity<InformatizationRequirementDetailsDTO> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<InformatizationRequirementDetailsDTO>() {}
        );
        return response.getBody();
    }

    public Integer saveRequirementDetailsDto(InformatizationRequirementDetailsDTO temp) {
        if(temp.getBasic().getInformatizationRequirementIndex()!=null
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"informatization/requirementDetailsDto";
            HttpEntity<InformatizationRequirementDetailsDTO> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }



}
